
import {transformI18n} from "@/plugins/i18n";
import type {PaginationProps} from "@pureadmin/table";
import {LotteryLists,LotteryLogs} from "@/api/system";
import {type Ref, reactive, ref, onMounted, h, toRaw} from "vue";
import {getSalesList} from "@/api/crm";

export function useAccount(tableRef: Ref) {
  const form = reactive({
    name: "",
    code: "",
    tab_id: "1",
    audit_status: "3",time:[]
  });
  const curRow = ref();
  const dataList = ref([]);
  const statusList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const isLinkage = ref(false);
  const isExpandAll = ref(false);
  const isSelectAll = ref(false);
  const salesman_arr = ref([]);


  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,pageSizes: [20, 40, 60,100,300,500],
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    { label: "奖项Id", prop: "id", width: 80, sortable: "custom",align: "left"},
    {label: "活动Id", prop: "activity_id",width:150,showOverflowTooltip:true,sortable: "custom",},
    {label: "奖项名称", prop: "name",width:150,showOverflowTooltip:true,sortable: "custom",},
    {label: "奖品图片", prop: "image",width:150,showOverflowTooltip:true,cellRenderer: ({ row, index }) => (
      <el-image style="width: 40px; height: 40px" src={row.image} preview-src-list={[row.image]} preview-teleported={true} />
    )},
    {label: "奖项比例", prop: "percent", width: 120,sortable:true,cellRenderer: ({ row, index }) => (
        <>{row.percent}%</>
      )},
    {label: "奖项说明", prop: "remark", sortable:true},
    {label: "排序", prop: "rank", width: 140,sortable:true},
    {label: "剩下数量", prop: "left_num", width: 120,sortable:true},
    {label: "创建时间", prop: "created_at", width: 120,sortable:true},

  ];
  const columns1: TableColumnList = [
    { label: "奖项Id", prop: "id", width: 80, sortable: "custom",align: "left"},
    {label: "活动Id", prop: "activity_id",width:80,showOverflowTooltip:true,sortable: "custom",},
    {label: "奖项id", prop: "lottery_id",width:80,showOverflowTooltip:true,sortable: "custom",},
    {label: "奖项名称", prop: "lottery_name",width:150,showOverflowTooltip:true,sortable: "custom",},
    {label: "中奖用户邮箱", prop: "email",width:250,showOverflowTooltip:true,sortable: "custom",},
    {label: "跟进业务员", prop: "creator",width:110,sortable: "custom",},
    {label: "奖品图片", prop: "image",width:140,showOverflowTooltip:true,cellRenderer: ({ row, index }) => (
        <el-image style="width: 40px; height: 40px" src={row.lottery?.image} preview-src-list={[row.lottery?.image]} preview-teleported={true} />
      )},
    {label: "奖项说明", prop: "lottery.remark", sortable:true},
    {label: "抽奖时间", prop: "created_at", width: 120,sortable:true},

  ];


  function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  }
  const today = new Date();
  const last30Days = new Date(today);
  last30Days.setDate(today.getDate() - 29);
  form.time = [formatDate(last30Days),formatDate(today)]

  function handleSizeChange(val: number) {
    pagination.pageSize = val
    onSearch()
  }
  function handleCurrentChange(val: number) {
    pagination.currentPage = val
    onSearch()
  }

  const sortChange = (k: any) => {
    form.sortField = k.prop
    form.order = k.order=='descending'?'desc':'asc'
    onSearch()
  }

  async function onSearch() {
    loading.value = true;
    form.page = pagination.currentPage
    form.limit = pagination.pageSize
    if(form.tab_id==1){
      const {data} = await LotteryLists(toRaw(form), 'get');
      dataList.value = data.data;
      pagination.total = data.total;
      pagination.pageSize = data.per_page;
      pagination.currentPage = data.current_page;
    }else{
      const {data} = await LotteryLogs(toRaw(form), 'get');
      dataList.value = data.data;
      pagination.total = data.total;
      pagination.pageSize = data.per_page;
      pagination.currentPage = data.current_page;
    }

    // statusList.value = data.status_arr;
    // searchCondList.value = data?.search_cond_arr;


    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  const getSalemanList = async function (page=1,limit=1000) {
    const {data} = await getSalesList({page,limit}); // 这里是返回一维数组结构，前端自行处理成树结构，返回格式要求：唯一id加父节点parentId，parentId取父节点id
    salesman_arr.value = data?.data;
  }



  /** 数据权限 可自行开发 */
  const searchCondList =  ref([])




  const filterMethod = (query: string, node) => {
    return transformI18n(node.title)!.includes(query);
  };

  onMounted(async () => {
    onSearch();
    getSalemanList();
  });


  return {
    form, isShow, curRow, loading, columns, dataList, isLinkage, pagination, isExpandAll, isSelectAll,
    statusList,salesman_arr,columns1,
    onSearch, resetForm,sortChange,
    searchCondList,
     filterMethod, transformI18n, handleSizeChange,
    handleCurrentChange
  };
}
